<template>
    <l-table-edit ref="TableEdit" :tableColumns="residentEnterpriseTableColmuns" :tableData="tableData" @table="tableFunc">
    </l-table-edit>
</template>


<script>
import { residentEnterpriseTableColmuns, detailApi } from "./columns"
export default {
    data() {
        return {
            tableLoading: true,
            residentEnterpriseTableColmuns,
            detailApi,
            tableData: [],
            tableTotal: 0,
            tableCurrentPage: 1,
            tablePageSize: 50,
        }
    },
    props: {
        landFormData: {
            type: Object,
            default: () => { }
        },
        options: {
            type: Object,
            default: () => { }
        }
    },
    mounted() {
        // 每次点击编辑都调到init方法
        // this.init()
    },
    methods: {
        async handleSave(data) {
            // 字段全部为空是否进行处理，该怎么处理
            // const { buildingArea, companyTypeCode, contactName, contactTel, coverArea, investDate, muTax, name, plotRatio, registeredCapital, residence, year } = data;
            // buildingArea.trim() === "" && companyTypeCode.trim() === "" && contactName.trim() === "" && contactTel.trim() === "" && coverArea.trim() === "" && investDate.trim() === "" && muTax.trim() === "" && name.trim() === "" && plotRatio.trim() === "" && registeredCapital.trim() === "" && residence.trim() === "" && year.trim() === ""
            if (0) {
                this.$message({
                    type: "error",
                    message: "请填入合法字段"
                })
            } else {
                // this.$set(data, "occupationType", data.landCompanyRel.occupationType);
                // this.$set(data, "residence", data.landCompanyRel.residence);
                // data.occupationType = data.landCompanyRel.occupationType;
                // data.residence = data.landCompanyRel.residence;
                this.$refs.TableEdit.handleCloseEdit(data) // 表格 关闭编辑
                this.$message({
                    type: "success",
                    message: '保存成功',
                });
            }
        },
        handleDeleteRow(row, index) {
            this.$confirm(this.$t('是否删除该条数据?'), this.$t('提示'), {
                confirmButtonText: this.$t('确定'),
                cancelButtonText: this.$t('取消'),
                type: 'warning'
            }).then(() => {
                this.tableData.splice(index, 1);
                this.$message({
                    type: 'success',
                    message: this.$t('删除成功!')
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: this.$t('已取消删除!')
                })
            })
        },
        tableFunc(res) {
            switch (res.type) {
                case 'save':
                    this.handleSave(res.data)
                    break;
                case 'delete':
                    this.handleDeleteRow(res.data, res.index)
                    break;
                case 'push':
                    this.tableData.push({
                        buildingArea: '',
                        companyTypeCode: '',
                        contactName: '',
                        contactTel: '',
                        coverArea: '',
                        investDate: '',
                        muTax: '',
                        name: '',
                        plotRatio: '',
                        registeredCapital: '',
                        residence: '',
                        year: '',
                        isEditing: true,
                    })
                    break;
                default:
                    break;
            }
        },
        init() {
            this.options.isAdd ? this.tableLoading = false : this.loadTableData();
        },
        filterBtns(row, btns) {
            return btns
        },
        turnTablePage({ rows }) {
            this.tablePageSize = rows
            this.loadTableData(true)
        },
        async loadTableData(isNotFirst) {
            if (!isNotFirst) {
                this.tableCurrentPage = 1
            }
            this.tableLoading = true
            let data = await detailApi.get(this.landFormData.id);
            data = data.data;
            if (data.code === 200) {
                this.tableData = data.data.companyList;
                this.tableData.forEach(i => {
                    this.$set(i, "occupationType", i.landCompanyRel?.occupationType || '')
                    this.$set(i, "residence", i.landCompanyRel?.residence || '')
                    this.$set(i, "isEditing", false)
                })
                this.tableTotal = data.data.companyList.length
            }
            else {
                this.tableTotal = 0
                this.tableData = []
            }
            this.tableLoading = false
        },
    }
}
</script>


<style scoped lang='scss'></style>